package client.login;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;

import javax.swing.JSeparator;

/**
 * Window to get user name and password. Application must supply a login object, and listen for changes to it.
 * 
 * @author Erik - logic
 * @author Fredrik - gui
 *
 */
public class LoginWindow extends JFrame implements PropertyChangeListener, KeyListener{

	private JPanel mainPanel;
	private JPasswordField passwordField;
	private JTextField userNameField;	
	private Login login;
	private JTextField ipPortField;
	private JTextField ipAddressField;
		

	/**
	 * Submit the changes to the login object.
	 */
	private void submitLogin() {
		String userName = userNameField.getText();
		String password = String.valueOf(passwordField.getPassword());
		String ipAddress = ipAddressField.getText();
		int ipPort;
		try {
			ipPort = Integer.parseInt(ipPortField.getText());
		}
		catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null, "Portnummer må være et gyldig heltall", "Feil", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		try {
			login.setInfo(userName, password, ipAddress, ipPort);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Feil", JOptionPane.ERROR_MESSAGE);
			passwordField.setText("");
			return;
		}
		this.dispose();
	}
	
	/**
	 * Create the frame.
	 */
	public LoginWindow(Login login) {
		setIconImage(Toolkit.getDefaultToolkit().getImage("resources/icons/aCalendarIcon.png"));
		setTitle("Logg inn");
		this.login = login;
		login.addPropertyChangeListener(this);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 395, 194);
		mainPanel = new JPanel();
		mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(mainPanel);
		
		JButton loginButton = new JButton("Logg inn");
		loginButton.setBounds(165, 131, 93, 23);
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				submitLogin();
			}
		});
		mainPanel.setLayout(null);
		mainPanel.add(loginButton);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(165, 94, 122, 30);
		mainPanel.add(passwordField);
		
		userNameField = new JTextField();
		userNameField.requestFocusInWindow();
		userNameField.setBounds(165, 63, 122, 30);
		mainPanel.add(userNameField);
		userNameField.setColumns(10);
		
		JLabel lblBrukernavn_1 = new JLabel("Brukernavn:");
		lblBrukernavn_1.setBounds(79, 65, 87, 14);
		mainPanel.add(lblBrukernavn_1);
		
		JLabel lblPassord = new JLabel("Passord:");
		lblPassord.setBounds(100, 96, 66, 14);
		mainPanel.add(lblPassord);
		
		JPanel panel = new JPanel();
		panel.setBounds(44, 6, 302, 39);
		mainPanel.add(panel);
		panel.setLayout(null);
		
		JLabel lblIpadresse = new JLabel("IP-adresse:");
		lblIpadresse.setBounds(6, 12, 93, 15);
		panel.add(lblIpadresse);
		
		ipPortField = new JTextField();
		ipPortField.setBounds(219, 6, 75, 27);
		panel.add(ipPortField);
		ipPortField.setColumns(10);
		
		JLabel label = new JLabel(":");
		label.setBounds(214, 12, 60, 15);
		panel.add(label);
		
		ipAddressField = new JTextField();
		ipAddressField.setBounds(85, 6, 122, 27);
		panel.add(ipAddressField);
		ipAddressField.setColumns(10);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(36, 49, 310, 9);
		mainPanel.add(separator);
		
		passwordField.addKeyListener(this);
		userNameField.addKeyListener(this);
		ipAddressField.addKeyListener(this);
		ipPortField.addKeyListener(this);
		
		ipAddressField.setText("78.91.4.242");
		ipPortField.setText("28095");
		
	}

	/**
	 * Listen for changes to the login object.
	 */
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getSource() == login) {
			userNameField.setText(login.getUsername());
			passwordField.setText(login.getPassword());
		}
		
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyCode() == KeyEvent.VK_ENTER) {
			submitLogin();
		} else if (arg0.getKeyCode() == KeyEvent.VK_F8) {
			userNameField.setText("gruppe2");
			passwordField.setText("aCalendar");
			submitLogin();
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {

	}
}
